package com.aigc.sclassroom.controller;

import com.aigc.sclassroom.pojo.Result;
import com.aigc.sclassroom.pojo.Survey;
import com.aigc.sclassroom.service.FeedbackService;
import com.aigc.sclassroom.service.SurveyService;
import com.aigc.sclassroom.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/survey")
public class SurveyController {
    @Autowired
    private SurveyService surveyService;

    @Autowired
    private FeedbackService feedbackService;

    //在本课程添加问卷
    @PostMapping
    public Result add(@RequestParam String surveyName, @RequestParam Long courseManageId){
        surveyService.add(surveyName,courseManageId);
        return Result.success();
    }

    //教师获取所有问卷
    @GetMapping("/listAll")
    public Result<List<Survey>> listAll(Long courseManageId){
        List<Survey> ls=surveyService.listAll(courseManageId);
        return Result.success(ls);
    }

    //学生获取该课程的所有问卷(state)
    @GetMapping("/stulistAll")
    public Result<List<Survey>> stuListAll(Long courseManageId){
        List<Survey> ls=surveyService.listAll(courseManageId);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Long userId =(id.longValue());
        for (int i = 0; i < ls.size(); i++) {
            Long surveyId = feedbackService.selectSurveyId(userId,ls.get(i).getSurveyId());
            if (surveyId!=null) {
                ls.get(i).setState("已填写");
            }
        }
        return Result.success(ls);
    }
}